package com.test3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;

public class Test5 {
	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
		String path = "C:/Users/domains90/Desktop/";
		String name = "黑龙江线所有村到县 .txt";
		
		String disExcel = path+"/hlj/";
		File disfile = new File(disExcel);
		disfile.mkdirs();	
		
		File file = new File(path+name);
		Test5 test5 = new Test5();
		List<Excel> list = new LinkedList<>();
		try(BufferedReader br = new BufferedReader(new InputStreamReader(
		new FileInputStream(file), "UTF-8"));){
			String lineTxt = null;
			while ((lineTxt = br.readLine()) != null) {	
				String[] info =  lineTxt.split(",");
				Excel excel = test5.new Excel();
				
				excel.setCcode(info[0]);
				excel.setCname(info[1]);
				
				excel.setAcode(info[2]);
				excel.setAname(info[3]);
				excel.setTcode(info[4]);
				excel.setTname(info[5]);
				excel.setVcode(info[6]);
				excel.setVname(info[7]);
				
				list.add(excel);
				System.out.println(lineTxt);				
			}			
		}
		Map<String, List<Excel>> map =  list.stream().collect(Collectors.groupingBy(Excel::getAcode));
		System.out.println(map.size());
		map.forEach((key,eles)->{
			if(eles.size()>0) {
				Excel excel = eles.get(0);
				String filename = excel.getCname()+"_"+ excel.getAname()+".xlsx";
				EasyExcel.write(disExcel+filename).withTemplate(path+"/XXX市县工作进度表.xlsx").sheet().doFill(eles);
			}
		});
	}
	
	public class Excel{
		
		private String cname;
		private String ccode;
		
		private String aname;
		private String acode;
		private int alevel;
		
		
		private String tname;
		private String tcode;
		private int tlevel;
		
		private String vname;
		private String vcode;
		private int vlevel;
		
		public String getAname() {
			return aname;
		}
		public void setAname(String aname) {
			this.aname = aname;
		}
		public String getAcode() {
			return acode;
		}
		public void setAcode(String acode) {
			this.acode = acode;
		}
		public String getTname() {
			return tname;
		}
		public void setTname(String tname) {
			this.tname = tname;
		}
		public String getTcode() {
			return tcode;
		}
		public void setTcode(String tcode) {
			this.tcode = tcode;
		}
		public int getTlevel() {
			return tlevel;
		}
		public void setTlevel(int tlevel) {
			this.tlevel = tlevel;
		}
		public String getVname() {
			return vname;
		}
		public void setVname(String vname) {
			this.vname = vname;
		}
		public String getVcode() {
			return vcode;
		}
		public void setVcode(String vcode) {
			this.vcode = vcode;
		}
		public int getVlevel() {
			return vlevel;
		}
		public void setVlevel(int vlevel) {
			this.vlevel = vlevel;
		}
		
		
		public String getCname() {
			return cname;
		}
		public void setCname(String cname) {
			this.cname = cname;
		}
		public String getCcode() {
			return ccode;
		}
		public void setCcode(String ccode) {
			this.ccode = ccode;
		}
		public int getAlevel() {
			return alevel;
		}
		public void setAlevel(int alevel) {
			this.alevel = alevel;
		}

	}
	
	

}
